#ifndef SC_RECEIVER_H
#define SC_RECEIVER_H

#include "common.h"

#include <stdbool.h>

#include "uhid/uhid_output.h"
#include "util/acksync.h"
#include "util/net.h"
#include "util/thread.h"

// receive events from the device
// managed by the controller
// controller管理的接收设备事件的接收器
struct sc_receiver {
    /* 这个socket和control中的socket是同一个 */
    sc_socket control_socket;

    // 接收器的线程信息
    sc_thread thread;
    sc_mutex mutex;

    struct sc_acksync *acksync;
    struct sc_uhid_devices *uhid_devices;

    const struct sc_receiver_callbacks *cbs;
    void *cbs_userdata;
};

struct sc_receiver_callbacks {
    void (*on_ended)(struct sc_receiver *receiver, bool error, void *userdata);
};

/*
初始化接收器
@param receiver
@return 初始化完成返回true,失败返回false
*/
bool
sc_receiver_init(struct sc_receiver *receiver, sc_socket control_socket,
                 const struct sc_receiver_callbacks *cbs, void *cbs_userdata);

void
sc_receiver_destroy(struct sc_receiver *receiver);

/*
接收器异步启动
@param receiver
@return 开启成功返回true,否则返回false
*/
bool
sc_receiver_start(struct sc_receiver *receiver);

// no sc_receiver_stop(), it will automatically stop on control_socket shutdown
// 等待接收线程运行结束
// @param receiver
void
sc_receiver_join(struct sc_receiver *receiver);

#endif
